
package com.ark.cloud.javacallnative.service.impl;

import com.alibaba.fastjson.JSON;
import com.ark.cloud.javacallnative.entity.*;
import com.ark.cloud.javacallnative.entity.callback.*;
import com.ark.cloud.javacallnative.enums.SdkStructType;
import com.ark.cloud.javacallnative.jni.JNICallNative;
import com.ark.cloud.javacallnative.modules.storage.service.*;
import com.ark.cloud.javacallnative.service.*;
import com.ark.cloud.javacallnative.util.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Objects;

/**
 * Copyright(c): 2021 Cloud-Ark Inc.
 * Author(s): Ma Wei (mawei@cloud-ark.com)
 */
@Service
@Slf4j
public class ConsumeServiceImpl {

    @Value("${kafka.host:}")
    private String[] kafkaHost;

    @Autowired
    private SensorDataService sensorDataService;
    @Autowired
    private PlatStateService platStateService;
    @Autowired
    private FireEventService fireEventService;
    @Autowired
    private ExplodeEventService explodeEventService;
    @Autowired
    private DetectEventService detectEventService;
    @Autowired
    private BGCMDService bgcmdService;
    @Autowired
    private  SensorOnOffEventService sensorOnOffEventService;
    @Autowired
    private CommunicationEventService communicationEventService;
    @Autowired
    private  DestoryEventService destoryEventService;
    @Autowired
    private  ScenarioDataService scenarioDataService;
    @Autowired
    private ContactStateService contactStateService;
    @Autowired
    private WaypointCmdService waypointCmdService;
    @Autowired
    private  SimEngineStateService simEngineStateService;
    @Autowired
    private  AircraftStatusService aircraftStatusService;
    @Autowired
    private ReplyCmdService replyCmdService;
    @Autowired
    private AircraftLaunchCmdService aircraftLaunchCmdService;
    @Autowired
    private SpeedCmdService speedCmdService;
    @Autowired
    private JammerCmdService jammerCmdService;
    @Autowired
    private GuideCmdService guideCmdService;
    @Autowired
    private LuaCmdService luaCmdService;
    @Autowired
    private LuaCmdReplyService luaCmdReplyService;

    @Autowired
    private ScenarioStateService scenarioStateService;

    @Autowired
    private JammerEventService jammerEventService;

    @Autowired
    private ReconnoiterCmdSerivce reconnoiterCmdSerivce;

    @Autowired
    private FireJGEventService fireJGEventService;
    @Autowired
    private FireDNEventService fireDNEventService;
    @Autowired
    private FireCKEventService fireCKEventService;
    @Autowired
    private EndGameJGEventService endGameJGEventService;
    @Autowired
    private EndGameDNEventService endGameDNEventService;
    @Resource
    private EndGameCKEventService endGameCKEventService;

    @Autowired
    private FireJGCmdService fireJGCmdService;
    @Autowired
    private FireDNCmdService fireDNCmdService;
    @Autowired
    private FireCKCmdService fireCKCmdService;
    @Autowired
    private SatelliteCmdService satelliteCmdService;

    @Autowired
    private SatOrbitService satOrbitService;


    @PostConstruct
    public void run() {
        JNICallNative.DataBus_Init();
        ArrayList<ServerInfo> servers = new ArrayList();
        for (String host : kafkaHost) {
            String[] split = host.split(":");
            ServerInfo server = new ServerInfo();
            server.setsServerIp(split[0]);
            server.setnServerPort(Integer.parseInt(split[1]));
            servers.add(server);
        }

        JNICallNative.DataBus_Register_Servers(servers);

        ArrayList<String> subStrings = new ArrayList();
        subStrings.add("PlatState");
        subStrings.add("SensorData");
        subStrings.add("ScenarioState");
        subStrings.add("ContactState");

        subStrings.add("FireEvent");
        subStrings.add("ExplodeEvent");

        subStrings.add("DetectEvent");
        subStrings.add("BGCMD");
        subStrings.add("SensorOnOffEvent");
        subStrings.add("CommunicationEvent");
        subStrings.add("DestoryEvent");
        subStrings.add("SimEngineState");
        subStrings.add("AircraftStatus");

        subStrings.add("ReplyCmd");
        subStrings.add("AircraftLaunchCmd");
        subStrings.add("SpeedCmd");
        subStrings.add("WaypointCmd");
        subStrings.add("JammerCmd");
        subStrings.add("GuideCmd");

        subStrings.add("FireJGEvent");
        subStrings.add("FireDNEvent");
        subStrings.add("FireCKEvent");
        subStrings.add("EndGameJGEvent");
        subStrings.add("EndGameDNEvent");
        subStrings.add("EndGameCKEvent");

//        // 6.11-6.14
        subStrings.add("FireJGCmd");
        subStrings.add("FireDNCmd");
        subStrings.add("FireCKCmd");
        subStrings.add("SatelliteCmd");

        JNICallNative.DataBus_Register_Subscribe_List(subStrings);
        JNICallNative.DataBus_Register_Publish_List(subStrings);

//        JNICallNative.DataBus_RegisterMsgHandler(new MyConsumer(), 10001);

        JNICallNative.DataBus_Register_PlatState_Handler(new OnPlateCallback(), 10101);

        JNICallNative.DataBus_Register_Sensor_Handler(new OnRecvSensorDataCallback(), 101001);
        JNICallNative.DataBus_Register_BGCMD_Handler(new OnRecvBGCMDCallback(), 101001);
        JNICallNative.DataBus_Register_Eevent_Handler(new OnRecvEventCallback(), 101001);

        JNICallNative.DataBus_Register_Cmd_Handler(new OnRecvCmdCallback(), 101001);
        JNICallNative.DataBus_Register_Scenario_Handler(new OnRecvScenarioCallback(), 101001);

        JNICallNative.DataBus_Register_ContactState_Handler(new OnRecvContactStateCallback(), 101001);

        JNICallNative.DataBus_Register_ScenarioState_Handler(new OnRecvScenarioStateCallbck(), 101001);
        JNICallNative.DataBus_Register_SatOrbit_Handler(new OnRecvSatOrbitCallback(), 101001);

        JNICallNative.DataBus_Start();

    }

    class OnPlateCallback implements OnPlatState {

        @Override
        public void OnPlate(PlatStateCallback platStateCallback) {

//            System.out.println(platStateCallback.toString());
            log.info(platStateCallback.toString());


            ArrayList<PlatState> platStates = platStateCallback.getPlatStates();

            for(PlatState platState : platStates){
                log.info("platState: {}", platState.toString());
                platStateService.savePlatStateInfo(platState);

            }
            log.info("platStates: {}", platStates.toString());


        }
    }


//    传感器数据
    class OnRecvSensorDataCallback implements OnSensorData {

        @Override
        public void OnRecvSensorData(SensorDataCallback sensorDataCallback) {
            System.out.println(sensorDataCallback.toString());

            ArrayList<SensorData> sensorDatas = sensorDataCallback.getSensorDatas();
            for(SensorData sensorData : sensorDatas){
                log.info("sensorData: {}", sensorData.toString());

                sensorDataService.saveSensorDataInfo(sensorData);

            }
            log.info("platStates: {}", sensorDatas.toString());


        }
    }

    //    时统
    class OnRecvScenarioStateCallbck implements OnScenarioState {

        @Override
        public void OnRecvScenarioState(ScenarioStateCallback scenarioStateCallback) {
            System.out.println(scenarioStateCallback.toString());

            ScenarioState scenarioState = scenarioStateCallback.getScenarioState();
            boolean save = scenarioStateService.save(scenarioState);
            log.info("save 时统数据: {}", save);
            log.info("scenarioState: {}", scenarioState.toString());


        }
    }

//    目标数据

    class OnRecvContactStateCallback implements OnContactState {

        @Override
        public void OnRecvContactState(ContactStateCallback contactStateCallback) {

            System.out.println(contactStateCallback.toString());

            ArrayList<ContactState> contactStates = contactStateCallback.getContactStates();
            for(ContactState contactState : contactStates){
                log.info("contactState: {}", contactState.toString());
                contactStateService.saveContactStateInfo(contactState);

            }
            log.info("contactStates: {}", contactStates.toString());


        }

    }

//    事件
    class OnRecvEventCallback implements OnEvent {

        @Override
        public void OnRecvEvent(EventAndCmdCallback eventAndCmdCallback) {

            System.out.println(eventAndCmdCallback.toString());

            int i = eventAndCmdCallback.getnCmdType();
            int dataLen = eventAndCmdCallback.getnDataLen();

            System.out.println("i:" + i);

            switch (SdkStructType.getByValue(i)) {
//                开火事件
                case E_FireEvent: {

                    FireEvent fireEvent = (FireEvent) eventAndCmdCallback.getpObj();
                    System.out.println(fireEvent.toString());

                    fireEventService.saveFireEventInfo(fireEvent);
                    log.info("fireEvent: {}", fireEvent.toString());

                    break;
                }
                // 爆炸事件
                case E_ExplodeEvent: {
                    ExplodeEvent explodeEvent = (ExplodeEvent) eventAndCmdCallback.getpObj();

                    log.info("explodeEvent: {}", JSON.toJSONString(explodeEvent));
                    explodeEventService.saveExplodeEventInfo(explodeEvent);

                    break;
                }

                 //探测成功事件
                case E_DetectEvent: {
                    DetectEvent detectEvent = (DetectEvent) eventAndCmdCallback.getpObj();

                    log.info("detectEvent: {}", JSON.toJSONString(detectEvent));
                    detectEventService.saveDetectEventInfo(detectEvent);
                    break;
                }
//                  传感器开/关机(事件/指令)
                case E_SensorOnOffEvent: {
                    SensorOnOffEvent sensorOnOffEvent = (SensorOnOffEvent) eventAndCmdCallback.getpObj();
                    log.info("sensorOnOffEvent: {}", JSON.toJSONString(sensorOnOffEvent));
                    sensorOnOffEventService.saveSensorOnOffEventInfo(sensorOnOffEvent);

                    break;
                }
//                通信事件
                case E_CommunicationEvent: {
                    CommunicationEvent communicationEvent = (CommunicationEvent) eventAndCmdCallback.getpObj();
                    log.info("communicationEvent: {}", JSON.toJSONString(communicationEvent));
                    communicationEventService.saveCommunicationEventInfo(communicationEvent);

                    break;
                }
//                平台销毁事件
                case E_DestoryEvent: {
                    DestoryEvent destoryEvent = (DestoryEvent) eventAndCmdCallback.getpObj();
                    log.info("destoryEvent: {}", JSON.toJSONString(destoryEvent));
                    destoryEventService.saveDestoryEventInfo(destoryEvent);

                    break;
                }

//                     干扰事件
                case E_JammerEvent: {

                    JammerEvent jammerEvent = (JammerEvent) eventAndCmdCallback.getpObj();
                    log.info("jammerEvent: {}", JSON.toJSONString(jammerEvent));
                    jammerEventService.saveJammerEvent(jammerEvent);

                    break;
                }
//                仿真引擎状态变化事件
                case E_SimEngineState: {
                    SimEngineState simEngineState = (SimEngineState) eventAndCmdCallback.getpObj();
                    log.info("simEngineState: {}", JSON.toJSONString(simEngineState));

                    simEngineStateService.saveSimEngineStateInfo(simEngineState);
                    break;
                }
//                飞机状态变化事件
                case E_AircraftStatus: {
                    AircraftStatus aircraftStatus = (AircraftStatus) eventAndCmdCallback.getpObj();
                    log.info("aircraftStatus: {}", JSON.toJSONString(aircraftStatus));

                    aircraftStatusService.saveAircraftStatusInfo(aircraftStatus);

                    break;
                }
//                新加的 六个
                case E_FireJGEvent:
                {
                    FireJGEvent  fireJGEvent = (FireJGEvent)eventAndCmdCallback.getpObj();
                    log.info("fireJGEvent: {}", JSON.toJSONString(fireJGEvent));

                    fireJGEventService.saveFireJGEvent(fireJGEvent);

                    break;
                }
                case E_FireDNEvent:
                {
                    FireDNEvent  fireDNEvent = (FireDNEvent)eventAndCmdCallback.getpObj();
                    log.info("fireDNEvent: {}", JSON.toJSONString(fireDNEvent));

                    fireDNEventService.saveFireDNEvent(fireDNEvent);


                    break;
                }
                case E_FireCKEvent:
                {
                    FireCKEvent  fireCKEvent = (FireCKEvent )eventAndCmdCallback.getpObj();
                    log.info("fireCKEvent: {}", JSON.toJSONString(fireCKEvent));

                    fireCKEventService.saveFireCKEvent(fireCKEvent);

                    break;
                }
                case E_EndGameJGEvent:
                {
                    EndGameJGEvent  endGameJGEvent = (EndGameJGEvent )eventAndCmdCallback.getpObj();
                    log.info("endGameJGEvent: {}", JSON.toJSONString(endGameJGEvent));

                   endGameJGEventService.saveEndGameJGEvent(endGameJGEvent);


                    break;
                }

                case E_EndGameDNEvent:
                {
                    EndGameDNEvent  endGameDNEvent = (EndGameDNEvent )eventAndCmdCallback.getpObj();
                    log.info("endGameDNEvent: {}", JSON.toJSONString(endGameDNEvent));

                    endGameDNEventService.saveEndGameDNEvent(endGameDNEvent);


                    break;
                }

                case E_EndGameCKEvent:
                {
                    EndGameCKEvent endGameCKEvent = (EndGameCKEvent )eventAndCmdCallback.getpObj();
                    log.info("endGameCKEvent: {}", JSON.toJSONString(endGameCKEvent));

                    endGameCKEventService.saveEndGameCKEvent(endGameCKEvent);

                    break;
                }

                default:
                    break;
            }
        }
    }

//    命令

    class OnRecvCmdCallback implements OnCmd {

        @Override
        public void OnRecvCmd(EventAndCmdCallback eventAndCmdCallback) {

            System.out.println(eventAndCmdCallback.toString());

            switch (SdkStructType.getByValue(eventAndCmdCallback.getnCmdType())) {

//                 指令确认反馈
                case E_ReplyCmd: {
                    ReplyCmd replyCmd = (ReplyCmd) eventAndCmdCallback.getpObj();
                    log.info("replyCmd: {}", JSON.toJSONString(replyCmd));

                    replyCmdService.saveReplyCmdInfo(replyCmd);

                    break;
                }
//                飞机起/降指令
                case E_AircraftLaunchCmd: {
                    AircraftLaunchCmd aircraftLaunchCmd = (AircraftLaunchCmd) eventAndCmdCallback.getpObj();
                    log.info("aircraftLaunchCmd: {}", JSON.toJSONString(aircraftLaunchCmd));

                    aircraftLaunchCmdService.saveAircraftLaunchCmdInfo(aircraftLaunchCmd);

                    break;
                }
//                速度控制指令
                case E_SpeedCmd: {
                    SpeedCmd speedCmd = (SpeedCmd) eventAndCmdCallback.getpObj();
                    log.info("speedCmd: {}", JSON.toJSONString(speedCmd));

                    speedCmdService.saveSpeedCmdInfo(speedCmd);

                    break;
                }
//                航线指令
                case E_WaypointCmd: {

                    WaypointCmd waypointCmd = (WaypointCmd) eventAndCmdCallback.getpObj();
                    log.info("waypointCmd: {}", JSON.toJSONString(waypointCmd));

                    waypointCmdService.saveWaypointCmdInfo(waypointCmd);

                    break;
                }

//           干扰指令
                case E_JammerCmd: {
                    JammerCmd jammerCmd = (JammerCmd) eventAndCmdCallback.getpObj();
                    log.info("jammerCmd: {}", JSON.toJSONString(jammerCmd));

                    jammerCmdService.saveJammerCmdInfo(jammerCmd);

                    break;
                }

//                引导指令
                case E_GuideCmd: {
                    GuideCmd guideCmd = (GuideCmd) eventAndCmdCallback.getpObj();
                    log.info("guideCmd: {}", JSON.toJSONString(guideCmd));

                    guideCmdService.saveGuideCmdInfo(guideCmd);

                    break;
                }
//
                case E_LuaCmd: {
                    LuaCmd luaCmd = (LuaCmd) eventAndCmdCallback.getpObj();
                    log.info("luaCmd: {}", JSON.toJSONString(luaCmd));

                    luaCmdService.saveLuaCmdInfo(luaCmd);
                    break;
                }

                case E_LuaCmdReply: {
                    LuaCmdReply luaCmdReply = (LuaCmdReply) eventAndCmdCallback.getpObj();
                    log.info("luaCmdReply: {}", JSON.toJSONString(luaCmdReply));

                    luaCmdReplyService.saveLuaCmdReplyInfo(luaCmdReply);
                    break;
                }

//                侦察指令
                case E_ReconnoiterCmd: {
                    ReconnoiterCmd reconnoiterCmd = (ReconnoiterCmd) eventAndCmdCallback.getpObj();
                    log.info("reconnoiterCmd: {}", JSON.toJSONString(reconnoiterCmd));

                    reconnoiterCmdSerivce.saveReconnoiterCmd(reconnoiterCmd);
                    break;
                }

                case E_FireJGCmd:
                {
                    FireJGCmd fireJGCmd = (FireJGCmd )eventAndCmdCallback.getpObj();
                    log.info("fireJGCmd: {}", JSON.toJSONString(fireJGCmd));


                   fireJGCmdService.saveFireJGCmd(fireJGCmd);


                    break;
                }

                case E_FireDNCmd:
                {
                    FireDNCmd fireDNCmd = (FireDNCmd )eventAndCmdCallback.getpObj();
                    log.info("fireDNCmd: {}", JSON.toJSONString(fireDNCmd));

                    fireDNCmdService.saveFireDNCmd(fireDNCmd);


                    break;
                }

                case E_FireCKCmd:
                {
                    FireCKCmd fireCKCmd = (FireCKCmd) eventAndCmdCallback.getpObj();
                    log.info("fireCKCmd: {}", JSON.toJSONString(fireCKCmd));

                    fireCKCmdService.saveFireCKCmd(fireCKCmd);

                    break;
                }
                case E_SatelliteCmd:
                {
                    SatelliteCmd satelliteCmd = (SatelliteCmd)eventAndCmdCallback.getpObj();
                    log.info("satelliteCmd: {}", JSON.toJSONString(satelliteCmd));

                    satelliteCmdService.saveSatelliteCmd(satelliteCmd);

                    break;
                }

                default:
                    break;
            }

        }

    }

    class OnRecvSatOrbitCallback implements OnSatOrbit {

        @Override
        public void OnRecvSatOrbit(SatOrbitCallback satOrbitCallback) {
            System.out.println(satOrbitCallback.toString());

            ArrayList<SatOrbit> satOrbits = satOrbitCallback.getSatOrbits();
            log.info("satOrbits: {}", JSON.toJSONString(satOrbits));

            for(SatOrbit satOrbit : satOrbits){

                satOrbitService.saveSatOrbit(satOrbit);
            }

        }
    }


    class OnRecvScenarioCallback implements OnScenario {

        @Override
        public void OnRecvScenario(ScenarioCallback scenarioCallback) {
            System.out.println(scenarioCallback.toString());
            ScenarioData scenarioData = scenarioCallback.getScenarioData();
            log.info("scenarioData: {}", JSON.toJSONString(scenarioData));
            scenarioDataService.saveScenarioDataInfo(scenarioData);

        }
    }

//    变轨事件
    class OnRecvBGCMDCallback implements OnBGCMD {

        @Override
        public void OnRecvBGCMD(BGCMDCallback bgcmdCallback) {
            System.out.println(bgcmdCallback.toString());

            ArrayList<BGCMD> bgcmds = bgcmdCallback.getBgcmds();
            log.info("bgcmds: {}", bgcmds.toString());

            for(BGCMD bgcmd : bgcmds){
                log.info("bgcmd: {}", bgcmd.toString());
                bgcmdService.saveBGCMDInfo(bgcmd);
            }

        }
    }


}
